home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / quodlibet / qltk / window.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  13KB  |  306 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. import sys
  5. import os
  6. from gi.repository import Gtk, GObject, Gdk
  7. from quodlibet import config
  8. from quodlibet.qltk import get_top_parent, is_wayland
  9. from quodlibet.util import DeferredSignal
  10. from quodlibet.util import connect_obj
  11.  
  12. def should_use_header_bar():
  13.     settings = Gtk.Settings.get_default()
  14.     if not settings:
  15.         return False
  16.     if not None(settings.props, 'gtk_dialogs_use_header'):
  17.         return False
  18.     return None.get_property('gtk-dialogs-use-header')
  19.  
  20.  
  21. class Dialog(Gtk.Dialog):
  22.     """A Gtk.Dialog subclass which supports the use_header_bar property
  23.     for all Gtk versions and will ignore it if header bars shouldn't be
  24.     used according to GtkSettings.
  25.     """
  26.     
  27.     def __init__(self, *args, **kwargs):
  28.         if not should_use_header_bar():
  29.             kwargs.pop('use_header_bar', None)
  30.         super(Dialog, self).__init__(*args, **kwargs)
  31.  
  32.  
  33.  
  34. class Window(Gtk.Window):
  35.     '''Base window class the keeps track of all window instances.
  36.  
  37.     All active instances can be accessed through Window.windows.
  38.     By defining dialog=True as a kwarg binds Escape to close, otherwise
  39.     ^W will close the window.
  40.     '''
  41.     windows = []
  42.     _preven_inital_show = False
  43.     __gsignals__ = {
  44.         'close-accel': (GObject.SIGNAL_RUN_LAST | GObject.SIGNAL_ACTION, GObject.TYPE_NONE, ()) }
  45.     
  46.     def __init__(self, *args, **kwargs):
  47.         self._header_bar = None
  48.         dialog = kwargs.pop('dialog', True)
  49.         super(Window, self).__init__(*args, **kwargs)
  50.         type(self).windows.append(self)
  51.         self._Window__accels = Gtk.AccelGroup()
  52.         if dialog:
  53.             self.set_type_hint(Gdk.WindowTypeHint.DIALOG)
  54.         self.set_destroy_with_parent(True)
  55.         self.set_position(Gtk.WindowPosition.CENTER_ON_PARENT)
  56.         self.add_accel_group(self._Window__accels)
  57.         if not dialog:
  58.             self.add_accelerator('close-accel', self._Window__accels, ord('w'), Gdk.ModifierType.CONTROL_MASK, 0)
  59.         else:
  60.             (esc, mod) = Gtk.accelerator_parse('Escape')
  61.             self.add_accelerator('close-accel', self._Window__accels, esc, mod, 0)
  62.         connect_obj(self, 'destroy', type(self).windows.remove, self)
  63.  
  64.     
  65.     def set_default_size(self, width, height):
  66.         if self._header_bar:
  67.             if width != -1:
  68.                 width += min(width - 174, 56)
  69.             if height != -1:
  70.                 height += 84
  71.             
  72.         super(Window, self).set_default_size(width, height)
  73.  
  74.     
  75.     def use_header_bar(self):
  76.         '''Try to use a headerbar, returns the widget or None in case
  77.         GTK+ is too old or headerbars are disabled (under xfce for example)
  78.         '''
  79.         if not not (self._header_bar):
  80.             raise AssertionError
  81.         if not None():
  82.             return False
  83.         header_bar = None.HeaderBar()
  84.         header_bar.set_show_close_button(True)
  85.         header_bar.show()
  86.         old_title = self.get_title()
  87.         self.set_titlebar(header_bar)
  88.         if old_title is not None:
  89.             self.set_title(old_title)
  90.         self._header_bar = header_bar
  91.         self.set_default_size(*self.get_default_size())
  92.         return header_bar
  93.  
  94.     
  95.     def has_close_button(self):
  96.         '''Returns True in case we are sure that the window decorations include
  97.         a close button.
  98.         '''
  99.         if self.get_type_hint() == Gdk.WindowTypeHint.NORMAL:
  100.             return True
  101.         if None.name == 'nt':
  102.             return True
  103.         if None.platform == 'darwin':
  104.             return True
  105.         if None._header_bar is not None:
  106.             return self._header_bar.get_show_close_button()
  107.  
  108.     
  109.     def present(self):
  110.         '''A version of present that also works if not called from an event
  111.         handler (there is no active input event).
  112.         See https://bugzilla.gnome.org/show_bug.cgi?id=688830
  113.         '''
  114.         
  115.         try:
  116.             GdkX11 = GdkX11
  117.             import gi.repository
  118.         except ImportError:
  119.             super(Window, self).present()
  120.  
  121.         window = self.get_window()
  122.         if window:
  123.             timestamp = GdkX11.x11_get_server_time(window)
  124.             self.present_with_time(timestamp)
  125.         else:
  126.             super(Window, self).present()
  127.  
  128.     
  129.     def set_transient_for(self, parent):
  130.         '''Set a parent for the window.
  131.  
  132.         In case parent=None, fall back to the main window.
  133.  
  134.         '''
  135.         if parent:
  136.             pass
  137.         is_toplevel = parent.props.type == Gtk.WindowType.TOPLEVEL
  138.         if parent is None or not is_toplevel:
  139.             if parent:
  140.                 print_w('Not a toplevel window set for: %r' % self)
  141.             app = app
  142.             import quodlibet
  143.             parent = app.window
  144.         super(Window, self).set_transient_for(parent)
  145.  
  146.     
  147.     def do_close_accel(self):
  148.         if isinstance(self.get_focus(), Gtk.Entry) and isinstance(self.get_focus().get_parent(), Gtk.TreeView):
  149.             self.get_focus().get_parent().grab_focus()
  150.             return None
  151.         if not None.emit('delete-event', Gdk.Event.new(Gdk.EventType.DELETE)):
  152.             self.destroy()
  153.  
  154.     
  155.     def prevent_inital_show(cls, value):
  156.         cls._preven_inital_show = bool(value)
  157.  
  158.     prevent_inital_show = classmethod(prevent_inital_show)
  159.     
  160.     def show_maybe(self):
  161.         '''Show the window, except if prevent_inital_show() was called and
  162.         this is the first time
  163.         '''
  164.         if not self._preven_inital_show:
  165.             self.show()
  166.  
  167.  
  168.  
  169. class PersistentWindowMixin(object):
  170.     '''A mixin for saving/restoring window size/position/maximized state'''
  171.     
  172.     def enable_window_tracking(self, config_prefix, size_suffix = ''):
  173.         '''Enable tracking/saving of changes and restore size/pos/maximized
  174.  
  175.         config_prefix -- prefix for the config key
  176.                          (prefix_size, prefix_position, prefix_maximized)
  177.         size_suffix -- optional suffix for saving the size. For cases where the
  178.                        window has multiple states with different content sizes.
  179.                        (example: edit tags with one song or multiple)
  180.  
  181.         '''
  182.         self._PersistentWindowMixin__state = 0
  183.         self._PersistentWindowMixin__name = config_prefix
  184.         self._PersistentWindowMixin__size_suffix = size_suffix
  185.         self._PersistentWindowMixin__save_size_deferred = DeferredSignal(self._PersistentWindowMixin__do_save_size, timeout = 50, owner = self)
  186.         self.connect('configure-event', self._PersistentWindowMixin__configure_event)
  187.         self.connect('window-state-event', self._PersistentWindowMixin__window_state_changed)
  188.         self.connect('notify::visible', self._PersistentWindowMixin__visible_changed)
  189.         self._PersistentWindowMixin__restore_window_state()
  190.  
  191.     
  192.     def __visible_changed(self, *args):
  193.         if not self.get_visible():
  194.             self._PersistentWindowMixin__restore_window_state()
  195.  
  196.     
  197.     def __restore_window_state(self):
  198.         if not is_wayland():
  199.             self._PersistentWindowMixin__restore_state()
  200.             self._PersistentWindowMixin__restore_position()
  201.         self._PersistentWindowMixin__restore_size()
  202.  
  203.     
  204.     def __conf(self, name):
  205.         if name == 'size':
  206.             name += '_' + self._PersistentWindowMixin__size_suffix
  207.         return '%s_%s' % (self._PersistentWindowMixin__name, name)
  208.  
  209.     
  210.     def __restore_state(self):
  211.         print_d('Restore state')
  212.         if config.getint('memory', self._PersistentWindowMixin__conf('maximized'), 0):
  213.             self.maximize()
  214.         else:
  215.             self.unmaximize()
  216.  
  217.     
  218.     def __restore_position(self):
  219.         print_d('Restore position')
  220.         pos = config.get('memory', self._PersistentWindowMixin__conf('position'), '-1 -1')
  221.         (x, y) = map(int, pos.split())
  222.         if x >= 0 and y >= 0:
  223.             self.move(x, y)
  224.  
  225.     
  226.     def __restore_size(self):
  227.         print_d('Restore size')
  228.         value = config.get('memory', self._PersistentWindowMixin__conf('size'), '-1 -1')
  229.         (x, y) = map(int, value.split())
  230.         screen = self.get_screen()
  231.         x = min(x, screen.get_width())
  232.         y = min(y, screen.get_height())
  233.         if x >= 1 and y >= 1:
  234.             self.resize(x, y)
  235.  
  236.     
  237.     def __configure_event(self, window, event):
  238.         self._PersistentWindowMixin__save_size_deferred(event.width, event.height)
  239.         return False
  240.  
  241.     
  242.     def __do_save_size(self, width, height):
  243.         if self._PersistentWindowMixin__state & Gdk.WindowState.MAXIMIZED:
  244.             return None
  245.         value = None % (width, height)
  246.         config.set('memory', self._PersistentWindowMixin__conf('size'), value)
  247.         if self.get_property('visible'):
  248.             pos_value = '%s %s' % self.get_position()
  249.             config.set('memory', self._PersistentWindowMixin__conf('position'), pos_value)
  250.  
  251.     
  252.     def __window_state_changed(self, window, event):
  253.         self._PersistentWindowMixin__state = event.new_window_state
  254.         if self._PersistentWindowMixin__state & Gdk.WindowState.WITHDRAWN:
  255.             return None
  256.         maximized = None(self._PersistentWindowMixin__state & Gdk.WindowState.MAXIMIZED)
  257.         config.set('memory', self._PersistentWindowMixin__conf('maximized'), maximized)
  258.  
  259.  
  260.  
  261. class _Unique(object):
  262.     '''A mixin for the window class to get a one instance per class window.
  263.     The is_not_unique method will return True if the window
  264.     is already there.
  265.     '''
  266.     _Unique__window = None
  267.     
  268.     def __new__(klass, *args, **kwargs):
  269.         window = klass._Unique__window
  270.         if window is None:
  271.             return super(_Unique, klass).__new__(klass, *args, **kwargs)
  272.         widgets = None((lambda x: isinstance(x, Gtk.Widget)), args)
  273.         if widgets:
  274.             parent = window.get_transient_for()
  275.             new_parent = get_top_parent(widgets[0])
  276.             if parent and new_parent and parent is not new_parent:
  277.                 window.set_transient_for(new_parent)
  278.                 window.hide()
  279.                 window.show()
  280.             
  281.         window.present()
  282.         return window
  283.  
  284.     
  285.     def is_not_unique(klass):
  286.         return bool(klass._Unique__window)
  287.  
  288.     is_not_unique = classmethod(is_not_unique)
  289.     
  290.     def __init__(self, *args, **kwargs):
  291.         if type(self)._Unique__window:
  292.             return None
  293.         type(self)._Unique__window = None
  294.         super(_Unique, self).__init__(*args, **kwargs)
  295.         connect_obj(self, 'destroy', self._Unique__destroy, self)
  296.  
  297.     
  298.     def _Unique__destroy(self, *args):
  299.         type(self)._Unique__window = None
  300.  
  301.  
  302.  
  303. class UniqueWindow(_Unique, Window):
  304.     pass
  305.  
  306.